<?php
class WeishopAction extends Action
{
	const PAGE_SIZE = 10;

	public function preMethod()
	{
		R("Mayijie/check_admin");
		$perm_key = session('admin_perm');
		$perm_arr = explode(",",$perm_key);
		if(!in_array(3,$perm_arr)){
			$this->error("无权操作");
		}
	}

	public function add()
	{
		$this->display();
	}

	public function checkDomain()
	{
		$map['domain'] = $this->_get('domain');
		$shop_id = $this->_get("shop_id");
		if(!empty($shop_id)){
			$map['shop_id'] = array("neq"=>$shop_id);
		}
		$WeiModel = new WeishopModel();
		$res = $WeiModel->getShopDetail($map);
		if(empty($res)){
			$res = array('errno' => 0);
		}else{
			$res = array('errno' => -1);
		}
		echo json_encode($res);
	}

	public function doAdd()
	{
		$pic = UploadModel::upload($_FILES,'./weishop/logo/',false);

		$data['domain'] = trim($this->_post("domain"));
		$data['shop_name'] = $this->_post('name');
		$data['level'] = $this->_post('level');
		$data['shop_intro'] = $this->_post('intro');
		$data['review'] = $this->_post('review');
		$data['shop_user'] = $this->_post('user');
		$data['shop_address'] = $this->_post('address');
		$data['shop_tel'] = $this->_post('tel');
		$data['end_time'] = strtotime($this->_post('end_time'));
		$data['create_time'] = time();
		$data['update_time'] = $data['create_time'];

		if(!empty($pic)){
			foreach($pic as $info){
				if($info['key'] == 'logo') $data['shop_logo'] = $info['savename'];
				if($info['key'] == 'banner') $data['shop_banner'] = $info['savename'];
			}
		}

		if(empty($data['shop_name'])){
			$this->error("店铺名称不得为空");
		}

		$WeiModel = new WeishopModel();
		$res = $WeiModel->addShop($data);
		if(!empty($res)){
			$this->success("添加成功");
		}else{
			$this->error("添加失败");
		}
	}

	public function lists()
	{
		$now_page = $this->_get('page');
		if(empty($now_page)){
			$now_page = 1;
		}
		$page_size = self::PAGE_SIZE;
		$WeiModel = new WeishopModel();
		$res = $WeiModel->getShopList(($now_page-1)*$page_size,$page_size,'',array("level"=>"desc"));
		$count = $WeiModel->getShopCount();

		$page = array(
			'count' => $count+$page_size,
			'page_size' => $page_size,
			'now_page' => $now_page,
			);
		$this->assign("page",$page);
		$this->assign("shoplist",$res);
		$this->display();
	}

	public function edit()
	{
		$map['shop_id'] = $this->_get('id');
		$WeiModel = new WeishopModel();
		$shop = $WeiModel->getShopDetail($map);
		$this->assign("shop",$shop);
		$this->display();
	}

	public function updateShop()
	{
		$pic = UploadModel::upload($_FILES,'./weishop/logo/',false);
		$map['shop_id'] = $this->_post('shop_id');
		$WeiModel = new WeishopModel();
		$shop = $WeiModel->getShopDetail($map);

		$data['domain'] = trim($this->_post("domain"));
		$data['shop_name'] = $this->_post('name');
		$data['level'] = $this->_post('level');
		$data['shop_intro'] = $this->_post('intro');
		$data['review'] = $this->_post('review');
		$data['shop_user'] = $this->_post('user');
		$data['shop_address'] = $this->_post('address');
		$data['shop_tel'] = $this->_post('tel');
		$data['end_time'] = strtotime($this->_post('end_time'));
		$data['update_time'] = time();

		if(!empty($pic)){
			foreach($pic as $info){
				if($info['key'] == 'logo'){
					$data['shop_logo'] = $info['savename'];
					@unlink("./weishop/logo/".$shop['shop_logo']);
				}
				if($info['key'] == 'banner'){
					$data['shop_banner'] = $info['savename'];
					@unlink("./weishop/logo/".$shop['shop_banner']);
				}
			}
		}

		$res = $WeiModel->updateShop($map,$data);
		if($res != 0){
			$this->success("修改成功");
		}else{
			$this->error("修改失败");
		}
	}

	public function del()
	{
		$WeiModel = new WeishopModel();
		$map['shop_id'] = $this->_get("id");
		$shop = $WeiModel->getShopDetail($map);
		@unlink("./weishop/logo/".$shop['shop_logo']);
		@unlink("./weishop/logo/".$shop['shop_banner']);
		$res = $WeiModel->delShop($map);
		if(!empty($res)){
			echo 1;
		}
	}
}